package com.zty.xiaomi.server.Service.Tribunal;

import com.zty.xiaomi.server.Entity.Evidence.Evidence;
import com.zty.xiaomi.server.Entity.Tribunal.Tribunal;
import com.zty.xiaomi.server.Mapper.TribunalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class TribunalServiceImp implements TribunalService {

    @Autowired
    private TribunalMapper tribunalMapper;

    @Override
    public int addTribunal(Tribunal tribunal) throws IOException {
        int num = tribunalMapper.addTribunal(tribunal);
        if (num != 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return num;
    }

    @Override
    public List<Tribunal> getTribunalsByStatus(int status) throws IOException {
        return tribunalMapper.getTribunalsByStatus(status);
    }

    @Override
    public int updateTribunalUserVoteById(int tribunal_id) throws IOException {
        int num = tribunalMapper.updateTribunalUserVoteById(tribunal_id);
        if (num != 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return num;
    }

    @Override
    public int updateTribunalStoreVoteById(int tribunal_id) throws IOException {
        int num = tribunalMapper.updateTribunalStoreVoteById(tribunal_id);
        if (num != 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return num;
    }


    @Override
    public int updateTribunalResultAndStatusById(int tribunal_id, String result, int status) throws IOException {
        int num = tribunalMapper.updateTribunalResultAndStatusById(tribunal_id,result,status);
        if (num != 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return num;
    }

    @Override
    public Tribunal getTribunalDetailById(int tribunal_id) throws IOException {
        return tribunalMapper.getTribunalDetailById(tribunal_id);
    }

    @Override
    public List<Tribunal> getMyTribunalByUserName(String user_name) throws IOException {
        return tribunalMapper.getMyTribunalByUserName(user_name);
    }

    @Override
    public List<Tribunal> getAllTribunal() throws IOException {

        return tribunalMapper.getAllTribunal();
    }

    @Override
    public int updateTribunalStatusById(int tribunal_id) {
        return tribunalMapper.updateTribunalStatusById(tribunal_id);
    }

}
